<template>
	<view class="shopping-cart-page">
		<!-- 顶部导航栏 -->
		<!-- <view class="header-nav">
      <view class="nav-content">
        <view class="back-btn" @click="goBack">
          <text class="iconfont-v2 icon-youjiantou back-icon" style="transform: rotate(180deg);"></text>
        </view>
        <text class="page-title">购物车</text>
        <view class="header-actions">
          <view class="clear-cart" @click="clearCart">
            <text class="iconfont-v2 icon-shanchu clear-icon"></text>
            <text class="clear-text">清空购物车</text>
          </view>
        </view>
      </view>
    </view> -->

		<!--  <view class="header-actions">
      <view class="clear-cart" @click="clearCart">
        <text class="iconfont-v2 icon-shanchu clear-icon"></text>
        <text class="clear-text">清空购物车</text>
      </view>
    </view> -->

		<!-- 购物车商品列表 -->
		<view class="cart-list">

			<view class="clear-cart" @click="clearCart">
				<text class="iconfont-v2 icon-shanchu clear-icon"></text>
				<text class="clear-text">清空购物车</text>
			</view>

			<view class="cart-item" v-for="(item, index) in cartItems" :key="index">
				<!-- 选择框 -->
				<view class="select-checkbox" @click="toggleSelect(index)">
					<text class="iconfont-v2 icon-weibiaoti--copy checkbox-icon" v-if="item.selected"></text>
					<text class="checkbox-placeholder" v-else></text>
				</view>

				<!-- 商品图片 -->
				<view class="product-image">
					<image :src="item.image" :alt="item.name" />
				</view>

				<!-- 商品信息 -->
				<view class="product-info">
					<text class="product-name">{{ item.name }}</text>
					<text class="special-tag" v-if="item.isSpecialOffer">特价</text>
					<view class="price-info">
						<text class="original-price">¥{{ item.originalPrice }}</text>
						<!-- <text class="current-price">¥{{ item.currentPrice }}</text> -->
					</view>
					<!-- 数量调节器 -->
					<view class="quantity-control">
						<text class="current-price">¥{{ item.currentPrice }}</text>
						<view class="quantity-box">
							<button class="quantity-btn minus-btn" @click="decreaseQuantity(index)">
								<text>-</text>
							</button>
							<input class="quantity-input" type="number" :value="item.quantity"
								@input="updateQuantity(index, $event.detail.value)" />
							<button class="quantity-btn plus-btn" @click="increaseQuantity(index)">
								<text>+</text>
							</button>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部结算栏 -->
		<view class="bottom-bar">
			<view class="select-all">
				<view class="select-checkbox" @click="toggleSelectAll">
					<text class="iconfont-v2 icon-weibiaoti--copy checkbox-icon" v-if="isAllSelected"></text>
					<text class="checkbox-placeholder" v-else></text>
				</view>
				<text class="select-all-text">全选</text>
			</view>
			<view class="total-info">
				<text class="total-text">合计：</text>
				<text class="total-price">¥{{ totalPrice }}</text>
			</view>
			<button class="checkout-btn" @click="goToCheckout">
				<text>去结算</text>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 购物车商品数据
				cartItems: [{
						id: 1,
						name: "鲜活 江苏洪泽湖南大闸蟹3对(母 215-235g, 公290-310g)",
						image: "http://img.1637.com/2015/07/10/YZS1VNIJE3HR504910.jpg",
						originalPrice: 45.8,
						currentPrice: 39.9,
						quantity: 1,
						selected: true,
						isSpecialOffer: true,
					},
					{
						id: 2,
						name: "四川眉山 爱媛38号果冻橙礼盒装",
						image: "http://img.1637.com/2015/07/10/YZS1VNIJE3HR504910.jpg",
						originalPrice: 45.8,
						currentPrice: 39.9,
						quantity: 1,
						selected: true,
						isSpecialOffer: true,
					},
				]
			}
		},
		computed: {
			// 计算属性
			isAllSelected() {
				return this.cartItems.every((item) => item.selected);
			},
			totalPrice() {
				return this.cartItems
					.filter((item) => item.selected)
					.reduce((total, item) => total + item.currentPrice * item.quantity, 0)
					.toFixed(1);
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			clearCart() {
				uni.showModal({
					title: '确认清空',
					content: '确定要清空购物车吗？',
					success: (res) => {
						if (res.confirm) {
							this.cartItems = [];
							uni.showToast({
								title: '购物车已清空',
								icon: 'success'
							});
						}
					}
				});
			},
			toggleSelect(index) {
				this.cartItems[index].selected = !this.cartItems[index].selected;
			},
			toggleSelectAll() {
				const newSelectState = !this.isAllSelected;
				this.cartItems.forEach(item => {
					item.selected = newSelectState;
				});
			},
			decreaseQuantity(index) {
				if (this.cartItems[index].quantity > 1) {
					this.cartItems[index].quantity--;
				}
			},
			increaseQuantity(index) {
				this.cartItems[index].quantity++;
			},
			updateQuantity(index, value) {
				const quantity = parseInt(value) || 1;
				this.cartItems[index].quantity = Math.max(1, quantity);
			},
			goToCheckout() {
				const selectedItems = this.cartItems.filter(item => item.selected);
				if (selectedItems.length === 0) {
					uni.showToast({
						title: '请选择要结算的商品',
						icon: 'none'
					});
					return;
				}

				console.log('去结算:', selectedItems);
				uni.navigateTo({
					url: '/pages/fast/checkout'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shopping-cart-page {
		padding: 28rpx;
		min-height: 100vh;
		background: #f5f5f5;
		padding-bottom: 160rpx;
	}

	.header-actions {
		background: white;
		padding: 20rpx 40rpx;
		display: flex;
		justify-content: flex-end;
		margin-bottom: 20rpx;

		// .clear-cart {
		//   display: flex;
		//   align-items: center;
		//   gap: 8rpx;
		//   cursor: pointer;
		//   transition: all 0.3s ease;

		//   &:hover {
		//     color: #ED6567;
		//   }

		//   .clear-icon {
		//     font-size: 28rpx;
		//     color: #666;
		//   }

		//   .clear-text {
		//     font-size: 26rpx;
		//     color: #666;
		//   }
		// }
	}

	.cart-list {
		background: white;
		padding: 30rpx;
		gap: 24rpx;
		display: flex;
		align-items: flex-start;
		justify-content: center;
		flex-direction: column;
		/* 子元素垂直排列 */
		box-shadow: 0 1px 7px rgba(0, 0, 0, 0.08);
		border-radius: 11px;

		.cart-item {
			// background: white;
			// margin: 0 20rpx 20rpx;
			// border-radius: 24rpx;
			padding: 30rpx 0rpx;
			display: flex;
			align-items: center;
			gap: 24rpx;
			// box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);

			.select-checkbox {
				width: 40rpx;
				height: 40rpx;
				border: 2rpx solid #ddd;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				cursor: pointer;
				transition: all 0.3s ease;

				&.selected {
					background: #ED6567;
					border-color: #ED6567;
				}

				.checkbox-icon {
					font-size: 36rpx;
					color: #0EA828;
				}
				// &.active {
				//   .select-checkbox .checkbox-icon {
				//     color: #0EA828;
				// 	border: none;
				//   }
				// }
				.checkbox-placeholder {
					width: 24rpx;
					height: 24rpx;
					border-radius: 50%;
					background: transparent;
				}
			}

			.product-image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 16rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
					object-fit: cover;
				}
			}

			.product-info {
				flex: 1;

				.product-name {
					font-size: 26rpx;
					color: #333;
					// margin-bottom: 12rpx;
					display: block;
					line-height: 1.4;
				}

				.special-tag {
					//background: #ED6567;
					border: 2rpx solid #ED6567;
					color: #F55726;
					padding: 4rpx 12rpx;
					border-radius: 10rpx;
					font-size: 20rpx;
					margin-bottom: 12rpx;
					display: inline-block;
				}

				.price-info {
					display: flex;
					align-items: center;
					gap: 16rpx;
					// margin-bottom: 20rpx;

					.original-price {
						font-size: 24rpx;
						color: #999;
						text-decoration: line-through;
					}


				}

				.quantity-control {
					display: flex;
					align-items: center;
					// gap: 16rpx;
					justify-content: space-between;
					.quantity-box {
						display: flex;
						align-items: center;
						justify-content: space-between;
						}
					.current-price {
						font-size: 32rpx;
						font-weight: 600;
						color: #ED6567;
					}

					.quantity-btn {
						width: 60rpx;
						height: 60rpx;
						// border: 2rpx solid #e0e0e0;
						background: #F8F9FA;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						cursor: pointer;
						transition: all 0.3s ease;

						&:hover {
							border-color: #ED6567;
							color: #ED6567;
						}

						text {
							font-size: 32rpx;
							font-weight: 600;
							color: #333;
						}
					}

					.quantity-input {
						width: 80rpx;
						height: 60rpx;
						// border: 2rpx solid #e0e0e0;
						border-radius: 8rpx;
						text-align: center;
						font-size: 28rpx;
						color: #333;
						background: white;

						&:focus {
							border-color: #ED6567;
							outline: none;
						}
					}
				}
			}
		}

		.clear-cart {
			display: flex;
			align-items: center;
			gap: 8rpx;
			width: 100%;
			// cursor: pointer;
			// transition: all 0.3s ease;
			justify-content: flex-end;

			&:hover {
				color: #ED6567;
			}

			.clear-icon {
				font-size: 28rpx;
				color: #F83D3D;
			}

			.clear-text {
				font-size: 26rpx;
				color: #F83D3D;
			}
		}
	}

	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: white;
		padding: 20rpx 40rpx;
		border-top: 2rpx solid #f0f0f0;
		display: flex;
		align-items: center;
		gap: 20rpx;

		.select-all {
			display: flex;
			align-items: center;
			gap: 16rpx;

			.select-checkbox {
				width: 40rpx;
				height: 40rpx;
				border: 2rpx solid #ddd;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				cursor: pointer;
				transition: all 0.3s ease;

				&.selected {
					background: #ED6567;
					border-color: #ED6567;
				}
				.checkbox-icon {
					font-size: 36rpx;
					color: #0EA828;
				}
				
				// &.active {
				//   .select-checkbox .checkbox-icon {
				//     color: #0EA828;
				// 	border: none;
				//   }
				// }
				.checkbox-placeholder {
					width: 24rpx;
					height: 24rpx;
					border-radius: 50%;
					background: transparent;
				}
			}

			.select-all-text {
				font-size: 28rpx;
				color: #333;
			}
		}

		.total-info {
			flex: 1;
			display: flex;
			align-items: center;
			gap: 8rpx;

			.total-text {
				font-size: 28rpx;
				color: #666;
			}

			.total-price {
				font-size: 32rpx;
				font-weight: 600;
				color: #ED6567;
			}
		}

		.checkout-btn {
			background: #ED6567;
			color: white;
			border: none;
			border-radius: 50rpx;
			padding: 24rpx 48rpx;
			font-size: 32rpx;
			font-weight: 600;
			cursor: pointer;
			transition: all 0.3s ease;

			&:hover {
				background: #d55a54;
			}

			text {
				color: white;
			}
		}
	}
</style>